package com.Interface.pa;

public class DynamicArray {
    private int[] array;
    private int size;

    public DynamicArray(int initialCapacity) {
        array = new int[initialCapacity];
        size = 0;
    }

    public void add(int value) {
        if (size == array.length) {
            // 动态扩容：创建一个新的数组，大小是原来的两倍
            int[] newArray = new int[array.length * 2];
            // 将旧数组的内容复制到新数组中
            System.arraycopy(array, 0, newArray, 0, size);
            // 更新数组引用
            array = newArray;
        }
        // 将新元素添加到数组中
        array[size++] = value;
    }

    public int get(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
        }
        return array[index];
    }

    public int size() {
        return size;
    }
}